package com.shenghuai.bclient.stores.util

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment

/**
 * Created by liulei
 * 2019/11/7 14:05
 * Activity 便捷辅助类
 */
object ActivityUtil {
    @JvmOverloads
    fun start(
        context: Context,
        clazz: Class<out Activity?>,
        b: Bundle? = null,
        singleTop: Boolean = false,
        options: Bundle? = null
    ) {

        val starter = Intent(context, clazz)
        if (b != null) starter.putExtras(b)
        if (context !is Activity) {
            starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }

        if (singleTop) {
            starter.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
        }

        try {
            context.startActivity(starter, options)
        } catch (ex: Throwable) {
            ex.printStackTrace()
        }
    }


    @JvmOverloads
    fun startForResult(
        context: Context,
        clazz: Class<out Activity?>,
        b: Bundle? = null,
        requestCode: Int,
    ) {

        val starter = Intent(context, clazz)
        if (b != null) starter.putExtras(b)
        if (context !is Activity) {
            starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }

        try {
            if (context is Activity)
                context.startActivityForResult(starter, requestCode)
            else context.startActivity(starter)
        } catch (ex: Throwable) {
            ex.printStackTrace()
        }
    }

    fun buildIntent(
        context: Context,
        clazz: Class<*>,
        b: Bundle? = null,
        singleTop: Boolean = false
    ): Intent {
        val intent = Intent(context, clazz)
        if (b != null)
            intent.putExtras(b)
        if (singleTop)
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
        return intent
    }
}

// todo 批量修改成这种方法
inline fun <reified T : Activity> Activity.startActivity(
    b: Bundle? = null,
    options: Bundle? = null
) {
    SafeUtils.safeRun {
        ActivityUtil.start(this, T::class.java, b, true, options)
    }
}

inline fun <reified T : Activity> Activity.launchActivityResult(
    requestCode: Int,
    b: Bundle? = null
) {
    SafeUtils.safeRun {
        ActivityUtil.startForResult(this, T::class.java, b, requestCode)
    }
}


// todo 批量修改成这种方法
inline fun <reified T : Activity> Fragment.startActivity(b: Bundle? = null) {
    SafeUtils.safeRun {
        if (contextValid()) {
            ActivityUtil.start(activity!!, T::class.java, b, true)
        }
    }
}

inline fun <reified T : Activity> Fragment.buildIntent(
    b: Bundle? = null,
    singleTop: Boolean = false
): Intent {
    val intent = Intent(requireContext(), T::class.java)
    if (b != null)
        intent.putExtras(b)
    if (singleTop)
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
    return intent
}

inline fun <reified T : Activity> Activity.buildIntent(
    b: Bundle? = null,
    singleTop: Boolean = false
): Intent {
    val intent = Intent(this, T::class.java)
    if (b != null)
        intent.putExtras(b)
    if (singleTop)
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
    return intent
}